package com.sep.team4;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.sep.team4.data.Course;
import com.sep.team4.data.DatabaseManager;
import com.sep.team4.data.Grade;
import com.sep.team4.data.Test;

public class SepFinalTeam4Activity extends Activity {
	public void onCreate(Bundle savedInstanceState) {
		testDatabase();
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_main_default);
		// all oauth handling is done in separate instance
		OAuthHandler oAuthHandler = OAuthHandler.getInstance();
		// oAuthHandler got to know about this instance for retrieving and
		// storing data
		oAuthHandler.setParent(this);
		// Intent i = this.getIntent();
		if (this.getIntent().getData() == null) {
			// 1st step in OAuth sequence: get request-token
			oAuthHandler.getRequestToken();
			if (!oAuthHandler.accessTokenAlreadyDefined()) {
				// 2nd step in OAuth sequence: authorize, and get response-key
				doAuthorizationInView();
			}
		}
	}

	private void testDatabase() {
		DatabaseManager manager = DatabaseManager.getInstance(this);
		manager.clearAll();
	}

	/**
	 * authorize using webview, and get response-token With received token,
	 * validate
	 */
	private void doAuthorizationInView() {
		// webview is created to do the authorization
		WebView webview = new WebView(this);
		webview.getSettings().setJavaScriptEnabled(true);
		webview.setVisibility(View.VISIBLE);
		setContentView(webview);
		/* WebViewClient must be set BEFORE calling loadUrl! */
		webview.setWebViewClient(new WebViewClient() {

			@Override
			public void onPageStarted(WebView view, String url, Bitmap bitmap) {
				Log.d("test", "onPageStarted : " + url);
				if (url.startsWith(OAuthHandler.CALLBACK_URL)) {
					// 3d step in OAuth sequence: exchange temporary key for
					// final token
					OAuthHandler.getInstance().getAccessToken(Uri.parse(url));
					// callback url is called, window can be closed
					view.setVisibility(View.INVISIBLE);
					startActivity(new Intent(SepFinalTeam4Activity.this,
							SepFinalTeam4Activity.class));
				}

			}
		});

		// 2nd step in OAuth sequence: authorize, and get response-key
		webview.loadUrl(OAuthHandler.getInstance().getAuthorizationUrl());
	}

	@Override
	public void onResume() {
		super.onResume();
		OAuthHandler oAuthDevice = OAuthHandler.getInstance();
		Uri uri = this.getIntent().getData();

		if (oAuthDevice.accessTokenAlreadyDefined()
				|| (uri != null && oAuthDevice.getScheme().equals(
						uri.getScheme()))) {
			// TODO: check of de database gevult is

			JsonHandler json = JsonHandler.getInstance(this.getIntent()
					.getData());

			Intent intent = new Intent();
			intent.setClass(this, MainTab.class);
			startActivity(intent);
			this.finish();
		}

	}

}
